package com.imddy.tweb.xml;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Iterator;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.http.conn.util.PublicSuffixMatcher;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.DOMBuilder;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

public class Jdom2Test {

	public void parseByJDOM(String file) throws Exception {
		SAXBuilder sb = new SAXBuilder();
		// 由于是web项目 ,并且想放在resources目录下
		Document doc = sb.build(new File(Jdom2Test.class.getClassLoader().getResource(file).toURI().getPath()));
		Element root = doc.getRootElement();
		List<Element> elements = root.getChildren();
		// List lineList = root.getChildren("line");
		for (Element element : elements) {
			String lid = element.getAttributeValue("lid");// 获取<line>元素的lid属性值
			String num = element.getAttributeValue("num");// 获取<line>元素的num属性值
			System.out.println("==lid:" + lid);
			System.out.println("==num:" + num);

			Element idElement = element.getChild("id");// 获得<line>下<id>标签下的子元素
			String id = idElement.getText();// // 获得<line>下<id>标签下的子元素值
			System.out.println("==路线id:" + id);

			List stationList = element.getChildren("station");// 获得<line>下<station>列表
			for (Iterator subIter = stationList.iterator(); subIter.hasNext();) {
				Element stationElement = (Element) subIter.next();// 获取<station>元素

				Element sidElement = stationElement.getChild("sid");// 获得<station>下<sid>标签下的子元素
				Element snameElement = stationElement.getChild("sname");// 获得<station>下<sname>标签下的子元素
				String sid = sidElement.getText();// 获得<station>下<sid>标签下的子元素值
				String sname = snameElement.getText();// 获得<station>下<sname>标签下的子元素值

				System.out.println("==路线sid:" + sid);
				System.out.println("==路线sname:" + sname);
			}
		}

	}

	public void writeXMLFile(String file) throws Exception {
		// 文档
		Document doc = new Document();

		// 根节点
		Element root = new Element("add");
		doc.setRootElement(root);

		// 子节点一
		Element element = new Element("doc");
		element.setAttribute("id", "1");
		Element name = new Element("field");
		name.setAttribute("name", "title");
		name.setText("北京欢迎您");
		Element content = new Element("field");
		content.setAttribute("name", "content");
		content.setText("北京真的欢迎您");
		element.addContent(name);
		element.addContent(content);
		root.addContent(element);

		// 子节点二
		element = new Element("doc").setAttribute("id", "2");
		name = new Element("field").setAttribute("name", "title");
		name.setText("上海也欢迎您");
		content = new Element("field").setAttribute("name", "content");
		content.setText("上海真的也欢迎您");
		element.addContent(name);
		element.addContent(content);
		root.addContent(element);

		// 输出到文件，输出流及格式都设置为UTF-8
		// 由于是web项目 ,并且想放在resources目录下,写的话就是在class目录下了
		System.out.println(Jdom2Test.class.getClassLoader().getResource("").toURI().getPath() + file);
		FileOutputStream fos = new FileOutputStream(Jdom2Test.class.getClassLoader().getResource("").toURI().getPath() + file);
		OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
		Format format = Format.getPrettyFormat();
		format.setEncoding("UTF-8");

		XMLOutputter output = new XMLOutputter();
		output.setFormat(format);
		output.output(doc, osw);
		osw.close();
		fos.close();
		System.out.println("wirterXMLFile...OK ");
	}

	public void CreateJdomFromDom() throws Exception {
		String xmlSource = "http://feeds.bbci.co.uk/news/technology/rss.xml?edition=int";

		// create the w3c DOM document from which JDOM is to be created
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		// we are interested in making it namespace aware.
		factory.setNamespaceAware(true);
		DocumentBuilder dombuilder = factory.newDocumentBuilder();

		org.w3c.dom.Document w3cDocument = dombuilder.parse(xmlSource);

		// w3cDocument is the w3c DOM object. we now build the JDOM2 object

		// the DOMBuilder uses the DefaultJDOMFactory to create the JDOM2
		// objects.
		DOMBuilder jdomBuilder = new DOMBuilder();

		// jdomDocument is the JDOM2 Object
		Document jdomDocument = jdomBuilder.build(w3cDocument);

		// The root element is the root of the document. we print its name
		System.out.println(jdomDocument.getRootElement().getName()); // prints
																		// "rss"
	}

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub

		// Jdom2Test jdom2Test = new Jdom2Test();
		// jdom2Test.parseByJDOM("testxml.xml");
		// jdom2Test.writeXMLFile("test1.xml");
		// jdom2Test.CreateJdomFromDom();
		
		SAXBuilder sb = new SAXBuilder();
		try {
			Document document = sb.build( Jdom2Test.class.getClassLoader().getResource("testxml.xml").toURI().getPath() );
			Element rootElement = document.getRootElement();
			List<Element> elementList = rootElement.getChildren();
			for (Element element : elementList) {
				System.out.println();
				System.out.println(element.getName());
				List<Element> chileList = element.getChildren();
				for (Element child : chileList) {
					System.out.println("元素名:" + child.getName());
					System.out.println("元素值:" + child.getValue());
				}
			}
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}
